<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	//query for complainservice List//
  if(isset($_POST['checkBtn']) || isset($_POST['checkBtnNew']) || isset($_POST['checkBtnOther']))
  {
    if(isset($_REQUEST['complainGroup']))
    {
      foreach($_REQUEST['complainGroup'] as $key => $value)
      {
        header("location:techAllocation.php?complainServiceId=".$key);
      }
    }
    elseif(isset($_REQUEST['newGroup']))
    {
      foreach($_REQUEST['newGroup'] as $key => $value)
      {
        header("location:techAllocation.php?newInstallationMasterId=".$key);
      }
    }
    else
    {
      if(isset($_REQUEST['otherGroup']))
      {
        foreach($_REQUEST['otherGroup'] as $key => $value)
        {
          header("location:techAllocation.php?otherServiceId=".$key);
        }
      }
    }
  } 
  
  if(isset($_POST['statusBtn']))
  {
    if(isset($_REQUEST['complainGroup']))
    {
      foreach($_REQUEST['complainGroup'] as $key => $value)
      {
        header("location:pendingReport.php?complainServiceId=".$key);
      }
    }
    elseif(isset($_REQUEST['newGroup']))
    {
      foreach($_REQUEST['newGroup'] as $key => $value)
      {
        header("location:pendingReport.php?newInstallationMasterId=".$key);
      }
    }
    else
    {
      if(isset($_REQUEST['otherGroup']))
      {
        foreach($_REQUEST['otherGroup'] as $key => $value)
        {
          header("location:pendingReport.php?otherServiceId=".$key);
        }
     }
    }
  } 
  
	/////////////////  Bill date View:Start
	if(isset($_REQUEST['fromDateYear']))
		$fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];
	else
		$fromDate  = "2011-00-00";
	
	if(isset($_REQUEST['toDateYear']))
		$toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'];
	else
		$toDate    = date("Y-m-32");
	/////////////////  Bill date View:Stop
  
  $complainCallDetailArray  = array();
  $complainCount = 0;
  $serviceTotal  = 0;
  $amountTotal   = 0;
  $print         = isset($_REQUEST['print']) ? $_REQUEST['print'] : 0;
  $printArray    = array();
  $differenciat  = "";
  $msg = '<tr><td align="center" colspan="10"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';
  $selectComplainEntry = "SELECT complainservice.complainServiceId,DATE_FORMAT(complainservice.complainDate,'%d-%m-%Y') AS complainDate,
                                 DATE_FORMAT(complainservice.targetedDate,'%d-%m-%Y') AS targetedDate,complainservice.description,complainservice.importantPoint,
                                 complainservice.serviceCharge,complainservice.amountCollected, complainservice.techAllocated,party.partyId,party.companyName,
                                 category.categoryId,category.categoryName
				  						      FROM complainservice
					 						      LEFT JOIN party      ON party.partyId           = complainservice.partyId
											      LEFT JOIN category   ON category.categoryId     = complainservice.categoryId
											     WHERE complainservice.status = 'A'
	                           AND complainservice.complainDate >= '".$fromDate."'
	                           AND complainservice.complainDate <= '".$toDate."'
											     ORDER BY complainservice.complainDate,party.companyName"; 
											      
  $selectComplainEntryResult = mysql_query($selectComplainEntry);
  while($complaneEntryRow = mysql_fetch_array($selectComplainEntryResult))
  { 
  	//pending days count :Start
  	$pendingDays                                                   = strtotime($complaneEntryRow['targetedDate']);
  	$currentdate                                                   = strtotime(date("d-m-Y"));
  	$finalDays                                                     = abs($pendingDays - $currentdate);
  	$numberDays                                                    = $finalDays/86400;
  	$complainCallDetailArray[$complainCount][$differenciat]        = intval($numberDays);
  	//pending days count :End
	  $complainCallDetailArray[$complainCount]['complainServiceId']  = $complaneEntryRow['complainServiceId'];
    $complainCallDetailArray[$complainCount]['companyName']        = $complaneEntryRow['companyName'];
    $complainCallDetailArray[$complainCount]['complainServiceId']  = $complaneEntryRow['complainServiceId'];
	  $complainCallDetailArray[$complainCount]['categoryName']       = wordwrap($complaneEntryRow['categoryName'],15,"<br />\n",TRUE);
	  $complainCallDetailArray[$complainCount]['complainDate']       = $complaneEntryRow['complainDate'];
	  $complainCallDetailArray[$complainCount]['targetedDate']       = $complaneEntryRow['targetedDate'];
	  $complainCallDetailArray[$complainCount]['description']        = $complaneEntryRow['description'];
	  $complainCallDetailArray[$complainCount]['importantPoint']     = $complaneEntryRow['importantPoint'];
	  $complainCallDetailArray[$complainCount]['serviceCharge']      = $complaneEntryRow['serviceCharge'];
	  $complainCallDetailArray[$complainCount]['techAllocated']      = $complaneEntryRow['techAllocated'];
	  $complainCallDetailArray[$complainCount]['amountCollected']    = $complaneEntryRow['amountCollected'];
	  $serviceTotal += $complaneEntryRow['serviceCharge'];
	  $amountTotal  += $complaneEntryRow['amountCollected'];
	  $complainCount++;
  }  
  //query for complainservice List//
  //query for newinstallationmaster and newinstallationdetail List //
  $newInstallDetailArray  = array();
  $newInstallCount        = 0;
  $msg = '<tr><td align="center" colspan="6"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';

  $selectNewInstall = "SELECT newinstallationmaster.newinstallationMasterId,newinstallationmaster.installationDate,
                              DATE_FORMAT(newinstallationmaster.installationDate,'%d-%m-%Y') AS installationDate,
                              newinstallationmaster.closeDate,
                              DATE_FORMAT(newinstallationmaster.targetedDate,'%d-%m-%Y') AS targetedDate,
                              newinstallationmaster.status,newinstallationmaster.techAllocated,party.partyId,party.companyName,category.categoryName,
                              newinstallationdetail.description
				  						   FROM newinstallationmaster
					 						   LEFT JOIN newinstallationdetail ON newinstallationdetail.newinstallationMasterId = newinstallationmaster.newinstallationMasterId
					 						   LEFT JOIN party                 ON party.partyId             = newinstallationmaster.partyId
											   LEFT JOIN category              ON category.categoryId       = newinstallationdetail.categoryId
											  WHERE newinstallationmaster.status = 'A'
	                        AND newinstallationmaster.installationDate >= '".$fromDate."'
	                        AND newinstallationmaster.installationDate <= '".$toDate."'
											  ORDER BY newinstallationmaster.newinstallationMasterId desc"; 
    $selectNewInstallResult = mysql_query($selectNewInstall);
	  while($newInstallRow = mysql_fetch_array($selectNewInstallResult))
	  {
	  	if(isset($newInstallDetailArray[$newInstallCount - 1]['newinstallationMasterId']) 
		     && $newInstallDetailArray[$newInstallCount - 1]['newinstallationMasterId']  == $newInstallRow['newinstallationMasterId'])// && date prev and current both same
		  {
		    $newInstallDetailArray[$newInstallCount]['display'] = 0;
		  }	 
		  else
		  {
		  	$newInstallDetailArray[$newInstallCount]['display'] = 1;
		  } 	
		  $newInstallDetailArray[$newInstallCount]['newinstallationMasterId'] = $newInstallRow['newinstallationMasterId'];
		  $newInstallDetailArray[$newInstallCount]['techAllocated']           = $newInstallRow['techAllocated'];
	    $newInstallDetailArray[$newInstallCount]['companyName']             = $newInstallRow['companyName'];
		  $newInstallDetailArray[$newInstallCount]['categoryName']            = $newInstallRow['categoryName'];
		  $newInstallDetailArray[$newInstallCount]['installationDate']        = $newInstallRow['installationDate'];
		  $newInstallDetailArray[$newInstallCount]['targetedDate']               = $newInstallRow['targetedDate'];
		  $newInstallDetailArray[$newInstallCount]['description']             = $newInstallRow['description'];
		  $newInstallCount++;
		  
	 } 
	 $otherArray=array();
	 $otherCount = 0;
	 $selectOtherService = "SELECT otherservice.otherServiceId,otherservice.partyId,otherservice.serviceType,otherservice.categoryId,otherservice.techAllocated,
                                 DATE_FORMAT(otherservice.complainDate,'%d-%m-%Y') AS complainDate,DATE_FORMAT(otherservice.targetedDate,'%d-%m-%Y') AS targetedDate,
                                 otherservice.description,otherservice.importantPoint,party.partyId,party.companyName,party.address,category.categoryName
				  						      FROM otherservice
					 						      LEFT JOIN party    ON party.partyId = otherservice.partyId
											      LEFT JOIN category ON category.categoryId = otherservice.categoryId
											     WHERE otherservice.status = 'A'
	                           AND otherservice.complainDate >= '".$fromDate."'
	                           AND otherservice.complainDate <= '".$toDate."'";
	  if(isset($_REQUEST['otherServiceId']) && $_REQUEST['otherServiceId'] > 0)
	  {
	  	$selectOtherService.= " AND otherservice.otherServiceId =".$_REQUEST['otherServiceId'];
	  }
	  $selectOtherService.= " ORDER BY party.companyName";
		$selectOtherServiceResult = mysql_query($selectOtherService);		
		$demoServiceTotal = 0;							    
		$demoAmountCollected = 0;							    
		while($selectOtherServiceRow = mysql_fetch_array($selectOtherServiceResult))
		{
		  
		  $otherArray[$otherCount]['otherServiceId'] = $selectOtherServiceRow['otherServiceId'];
		  $otherArray[$otherCount]['techAllocated']  = $selectOtherServiceRow['techAllocated'];
		  $otherArray[$otherCount]['companyName']    = $selectOtherServiceRow['companyName'];
		  $otherArray[$otherCount]['categoryName']   = $selectOtherServiceRow['categoryName'];
		  $otherArray[$otherCount]['serviceType']    = $selectOtherServiceRow['serviceType'];
		  $otherArray[$otherCount]['complainDate']   = $selectOtherServiceRow['complainDate'];
		  $otherArray[$otherCount]['targetedDate']   = $selectOtherServiceRow['targetedDate'];
		  $otherArray[$otherCount]['importantPoint'] = $selectOtherServiceRow['importantPoint'];
		  $otherArray[$otherCount]['description']    = $selectOtherServiceRow['description'];
		  $otherCount++;
		  //Print array start//
		  $printArray['serviceType']  = $selectOtherServiceRow['serviceType'];
		  $printArray['companyName']  = $selectOtherServiceRow['companyName'];
		  $printArray['address']      = $selectOtherServiceRow['address'];
		  $printArray['categoryName'] = $selectOtherServiceRow['categoryName'];
		  $printArray['complainDate'] = $selectOtherServiceRow['complainDate'];
		  $printArray['targetedDate'] = $selectOtherServiceRow['targetedDate'];
		  $printArray['description'] = $selectOtherServiceRow['description'];
		  $printArray['importantPoint'] = $selectOtherServiceRow['importantPoint'];
		  //print array end//
		}
	//query for newinstallationmaster and newinstallationdetail List over //
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("serviceTotal",$serviceTotal);
  $smarty->assign("fromDate",$fromDate);
  $smarty->assign("toDate",$toDate);
  $smarty->assign("amountTotal",$amountTotal);
  $smarty->assign("complainCallDetailArray",$complainCallDetailArray);
  $smarty->assign("newInstallDetailArray",$newInstallDetailArray);
  $smarty->assign("otherArray",$otherArray);
  $smarty->assign("printArray",$printArray);
  $smarty->assign("differenciat",$differenciat);
  if($print == 1)
    $smarty->display("servicePrint.tpl");
  else
  $smarty->display("serviceList.tpl");
}
?>